package com.ayu.chatroom.service.impl;

import com.ayu.chatroom.exceptionHandler.CustomRuntimeException;
import com.ayu.chatroom.entity.FriendsRequest;
import com.ayu.chatroom.mapper.FriendsRequestMapper;
import com.ayu.chatroom.service.FriendsRequestService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ayu
 * @since 2022-10-10
 */
@Service
public class FriendsRequestServiceImpl extends ServiceImpl<FriendsRequestMapper, FriendsRequest> implements FriendsRequestService {

    @Override
    public void addRequest(String myUserId, String friendUserId) {
        //查一下有没有记录
        QueryWrapper<FriendsRequest> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("send_user_id", myUserId);
        queryWrapper.eq("accept_user_id", friendUserId);

        int count = this.count(queryWrapper);
        if (count > 0) {
            //全局异常捕获
            throw new CustomRuntimeException("已发送好友请求");
        }

        FriendsRequest friendsRequest = new FriendsRequest();
        friendsRequest.setSendUserId(myUserId);
        friendsRequest.setAcceptUserId(friendUserId);
        friendsRequest.setRequestDateTime(new Date());
        this.save(friendsRequest);
    }

    @Override
    public void deleteRequest(String acceptUserId, String sendUserId) {

        QueryWrapper<FriendsRequest> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("send_user_id",sendUserId);

        queryWrapper.eq("accept_user_id",acceptUserId);

        this.remove(queryWrapper);

    }
}
